package at.ac.tuwien.neue.g9;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.zxing.integration.android.IntentIntegrator;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends Activity
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final Button bGenerate = (Button) findViewById(R.id.button_generate);
        bGenerate.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 EditText e = (EditText) findViewById(R.id.edit_message);
                 
                 generateQR(v, e.getText().toString());
             }
        });
        
    }
    
    public void generateQR(View view, String text) {
        
        MessageDigest digest = null;
        String message;
        byte[] hash;
        
        try {
            digest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e1) {
            // TODO Error Handling
        }
        
        digest.reset();
        hash = digest.digest(text.getBytes());
        message = String.format("%0" + (hash.length*2) + "X", new BigInteger(1, hash));
        
        IntentIntegrator.encodeText(this, message);
    }
    
    
}
